import wx
import array
from log_object import *

def makeNew(parent, window, param1):
    """ this is how we instantiate the device object from
        the main program that loads this plugin """
    return HeatmapGraph(parent, window, param1)

class HeatmapGraph:
	def __init__(self, parent, window, param1):
		self.parent = parent
		self.window = window

		self.type = HEATMAP
		self.param1 = param1
		self.param2 = NOT_DEFINED 

		self.log_date = ""
		self.start = 0
		self.end = 0 
		self.panel_end = 0 
		self.dim = [] 
		self.space = [] 

	def draw(self, dc, start, end, dim, space):
		self.start = start
		self.end = end 
		self.panel_end =  end 
		self.dim = dim
		self.space = space

		foreground = self.parent.getForegroundColor()
		background = self.parent.getBackgroundColor()

		dc.SetBrush(wx.TRANSPARENT_BRUSH)
		dc.SetPen(wx.Pen(foreground, 1))
		dc.SetTextBackground(background)
		dc.SetTextForeground(foreground)
		font = wx.Font(15, wx.TELETYPE, wx.NORMAL, wx.BOLD)
		dc.SetFont(font)

		window_width = int(self.window.Width / space[0])
		window_height = int(self.window.Height / space[1])

		if self.param1 == LOG_POINTER_MOVE: 
			self.draw_pointer(dc, window_width, window_height)
		elif self.param1 == LOG_POINTER_MERGED:
			self.draw_merged_pointer(dc, window_width, window_height)
		elif self.param1 == LOG_POINTER_COLOR:
			self.draw_diff_pointer(dc, window_width, window_height)
		else: 
			desc1 = "window" 
			if self.param1 == LOG_WINDOW_NEW: 
				desc2 = "new area" 
			elif self.param1 == LOG_WINDOW_CHANGE: 
				desc2 = "movement" 

			self.draw_window(dc, window_width, window_height)


	def draw_pointer(self, dc, window_width, window_height):
		no_user = len(self.parent.selected_user_list)
		no_log =  len(self.parent.log_list)
		if no_user == 1 and no_log > 1 :
			no_user = no_log

		row_height = self.window.Height
		if no_user > 0 : 
			row_height = int((self.window.Height - 61) / no_user)  

		foreground = self.parent.getForegroundColor()
		filling_range = self.parent.effectRange
		half_range = int(filling_range /2)
		log_idx = 0 
		prev_height = 0
		for current_log in self.parent.log_list :

			ratio =  (current_log.display_width * 1.0) / current_log.display_height
			spare = int(window_width * 0.15)
			portion = 0.7
			if self.dim[0] == 1 or self.space[0] >= 2: 
				spare = 10 
				portion = 0.5

			graph_width = (window_width -spare) -50
			graph_height = int(graph_width / ratio)
			temp_height = window_height - 20
			label_height = window_height
			if no_user > 0 :
				temp_height = int((window_height - (no_user +1) *10) / no_user)
				label_height = window_height / no_user
			if graph_height > temp_height: 
				graph_height = temp_height 
				graph_width =  int(graph_height * ratio)
				print "modified.....", graph_width
			row_height =  row_height - graph_height  
			if self.space[0] == 1 :
				row_height = 0

			# 1. create buffer, 'B' means unsigned char, and create heatmap
			heatmap = []
			for j in range(graph_height) : 
				for i in range(graph_width) : 
					heatmap.append(0)


			log_end = current_log.duration
			self.log_date = current_log.date
			if self.panel_end == - 1 :
				self.end = -1
			if self.end == -1 or self.end > current_log.duration:
				self.end = current_log.duration

			x = (self.dim[0] * window_width) + int((window_width -graph_width) * portion)

			for pointer in current_log.pointer_list: 
				if self.parent.isSelectedUser(pointer.name) == False:
					continue

				y = prev_height +  (self.dim[1] * window_height) + 10  + int(row_height / 2.0)
				# draw title
				if self.space[0] == 1 : 
					len_name = len(pointer.name) * 10 + 20
					dc.DrawText(pointer.name, x - len_name , y + graph_height - 20)
				max = 0
				min = 999

				dc.SetBrush(wx.TRANSPARENT_BRUSH)
				dc.SetPen(wx.Pen(foreground, 1))
				dc.DrawRectangle(x, y, graph_width, graph_height)

				# 2. clean heatmap data structure
				idx = 0
				for j in range(graph_height) : 
					for i in range(graph_width) : 
						heatmap[idx] = 0
						idx += 1

				stroke_start = -1
				prev_time = -1
				noOfStroke = 0
				noOfClick = 0
				noOfDClick = 0
				noOfRClick = 0
				noOfDrag = 0
				move_count = 0
				click_flag = False
				# 3. update heatmap
				for log in pointer.log_list: 
					if log.mode == LOG_POINTER_NEW:
						prev_time = log.time
						click_flag = False
						move_count = 0
						stroke_start = log.time
					elif log.mode == LOG_POINTER_REMOVE:
						noOfStroke += 1
						prev_time = -1
						stroke_start = -1
						move_count = 0
					elif log.mode == LOG_POINTER_CLICK:
						click_flag = True
						move_count = 0
					elif log.mode == LOG_POINTER_DCLICK:
						noOfDClick += 1
					elif log.mode == LOG_POINTER_RCLICK:
						noOfRClick += 1
					elif log.mode == LOG_POINTER_RELEASE :
						if move_count == 1 :
							noOfDrag += 1
						else :
							if click_flag == True :
								noOfClick += 1
						move_count = 0
						prev_time = -1
						stroke_start = -1
						click_flag = False

 					if log.mode == LOG_POINTER_MOVE:
						if click_flag == False :
							if prev_time == -1 :
								prev_time = log.time
								stroke_start = log.time
							else :
								diff_time = log.time - prev_time
								if diff_time <= 1.0 :
									prev_time = log.time
								else :
									noOfStroke += 1
									stroke_start = -1
									prev_time = -1
						else :
							move_count =1


						log_min = log.time / 60
						if log_min < self.start or log_min > self.end :
							continue

						a = int(1.0 * log.x * graph_width / current_log.display_width) - half_range 
						b = int(1.0 * log.y * graph_height / current_log.display_height) - half_range
						#print log.x, log.y, "(", current_log.display_width, current_log.display_height, ")", "," , a, b,  "(", graph_width, graph_height, ")"
						for j in range(filling_range) : 
							b1 = b + j 
							if b1 < 0 or b1 >= graph_height: 
								continue
							for i in range(filling_range) : 
								a1 = a + i
								if a1 < 0 or a1 >= graph_width: 
									#print a1, b1, "(", graph_width, graph_height, ")"
									continue
								idx = (b1 * graph_width) + a1
								heatmap[idx] += 1
								if heatmap[idx] > max:
									max = heatmap[idx]
								if heatmap[idx] < min:
									min = heatmap[idx]

				# 4. update array
				#print max, min
				div_gap = 10
				if max != 0:
					div_gap = int(200.0 /max)
				print max, min
				buffer = array.array('B')
				for j in range(graph_height) : 
					for i in range(graph_width) : 
						idx = (j * graph_width) + i 
						ptn = heatmap[idx]
						if ptn > 0 : 
							# for local
							temp = 255 - int(255.0 * ptn / max)
							buffer.extend((temp, temp, temp))
						else:
							buffer.extend((255, 255, 255))

				# 5. create & draw bitmap
				rgbbmp = wx.BitmapFromBuffer(graph_width, graph_height, buffer)
				del buffer 
				dc.DrawBitmap(rgbbmp, x, y, False)

				i = min	
				range_loop = max - min + 1 
				gap = range_loop / 10.0
				if range_loop > 10 :
					range_loop = 10 
				if gap <= 0 : 
					gap = 1
				for j in range(range_loop) :
					temp = 255 - int(i * div_gap)
					dc.SetBrush(wx.Brush(wx.Colour(temp, 0, 0)))
					dc.SetPen(wx.Pen(wx.Colour(temp, 0, 0), 1))
					dc.DrawRectangle(x + graph_width + 10, y + (j*15), 15, 15)
					i += gap

				prev_height = y + graph_height + row_height +  10
				
				font = wx.Font(12, wx.TELETYPE, wx.NORMAL, wx.BOLD)
				dc.SetFont(font)
				#x = (self.window.Width -graph_width)/2  + graph_width + 70 
				x_label = 20
				y_label = y
				dc.DrawText(self.log_date, x_label, y_label)

				dc.DrawText("stroke #: " + str(noOfStroke), x_label, y_label + 20)
				dc.DrawText("click #: " + str(noOfClick) + ", " + str(noOfRClick)+"(r)", x_label, y_label + 35)
				dc.DrawText("drag #: " + str(noOfDrag), x_label, y_label + 50)
				dc.DrawText("double click :" + str(noOfDClick), x_label, y_label + 65)

				dc.DrawText("start: " + str(self.start), x_label, y_label + 85)
				dc.DrawText("end: " + str(self.end), x_label, y_label + 100)
				dc.DrawText("     /" + str(self.panel_end), x_label, y_label + 115)
			log_idx += 1

			del heatmap


	def draw_window(self, dc, window_width, window_height):
		desc1 = "window" 
		desc2 = "new area" 
		if self.param1 == LOG_WINDOW_CHANGE: 
			desc2 = "movement" 

		no_log =  len(self.parent.log_list)

		row_height = self.window.Height
		if no_log > 0 : 
			row_height = int((self.window.Height - 61) / no_log)  

		foreground = self.parent.getForegroundColor()
		log_idx = 0
		prev_height = 0
		for current_log in self.parent.log_list :
			log_end = current_log.duration
			self.log_date = current_log.date
			if self.panel_end == - 1 :
				self.end = -1
			if self.end == -1 or self.end > current_log.duration:
				self.end = current_log.duration

			ratio =  (current_log.display_width * 1.0) / current_log.display_height
			spare = int(window_width * 0.15)
			portion = 0.7
			if self.dim[0] == 1 or self.space[0] >= 2: 
				spare = 10 
				portion = 0.5

			graph_width = (window_width -spare) -50
			graph_height = int(graph_width / ratio)
			secW_ratio = graph_width*1.0/current_log.display_width
			secH_ratio = graph_height*1.0/current_log.display_height
			print "ratio...",secW_ratio,secH_ratio
			temp_height = window_height - 20
			label_height = window_height
			if no_log > 0 :
				temp_height = int((window_height - (no_log +1) *10) / no_log)
				label_height = window_height / no_log
			if graph_height > temp_height: 
				graph_height = temp_height 
				graph_width =  int(graph_height * ratio)
			row_height =  row_height - graph_height  
			if self.space[0] == 1 :
				row_height = 0

			x = (self.dim[0] * window_width) + int((window_width -graph_width) * portion)
			y = prev_height + (self.dim[1] * window_height) + 10  + int(row_height / 2.0)

			# 1. create buffer, 'B' means unsigned char, and create heatmap
			heatmap = []
			for j in range(graph_height) : 
				for i in range(graph_width) : 
					heatmap.append(0)

			dc.SetBrush(wx.TRANSPARENT_BRUSH)
			dc.SetPen(wx.Pen(foreground, 1))
			dc.DrawRectangle(x, y, graph_width, graph_height)

			idx = 0
			max = 0
			min = 999
			noOfMove = 0
			if self.param1 == LOG_WINDOW_NEW: 
				for window in current_log.window_list: 
					# 3. update heatmap
					for log in window.log_list: 
 						if log.mode == LOG_WINDOW_NEW :
							log_min = log.time / 60
							if log_min < self.start or log_min > self.end :
								continue

							#print log.bottom, log.top, log.left, log.right

							j_start =  int(1.0 * (current_log.display_height - log.top) * graph_height / current_log.display_height)
							j_end =  int(1.0 * (current_log.display_height - log.bottom) * graph_height / current_log.display_height)
							for j in range(j_start, j_end):
								if j < 0 or j >= graph_height: 
									continue
								i_start =  int(1.0 * log.left * graph_width / current_log.display_width)
								i_end =  int(1.0 * log.right * graph_width / current_log.display_width)
								for i in range(i_start, i_end):
									if i < 0 or i >= graph_width: 
										continue
									idx = (j * graph_width) + i 
									heatmap[idx] += 1
									if heatmap[idx] > max:
										max = heatmap[idx]
									if heatmap[idx] < min:
										min = heatmap[idx]
					noOfMove += window.no_change
			elif self.param1 == LOG_WINDOW_CHANGE: 
				for window in current_log.window_list: 
					# 3. update heatmap
					for log in window.log_list: 
 						#if log.mode == LOG_WINDOW_NEW or log.mode == LOG_WINDOW_CHANGE :
 						if log.mode == LOG_WINDOW_CHANGE :
							log_min = log.time / 60
							if log_min < self.start or log_min > self.end :
								continue

							#print log.bottom, log.top, log.left, log.right

							j_start =  int(1.0 * (current_log.display_height - log.top) * graph_height / current_log.display_height)
							j_end =  int(1.0 * (current_log.display_height - log.bottom) * graph_height / current_log.display_height)
							for j in range(j_start, j_end):
								if j < 0 or j >= graph_height: 
									continue
								i_start =  int(1.0 * log.left * graph_width / current_log.display_width)
								i_end =  int(1.0 * log.right * graph_width / current_log.display_width)
								for i in range(i_start, i_end):
									if i < 0 or i >= graph_width: 
										continue
									idx = (j * graph_width) + i 
									heatmap[idx] += 1
									if heatmap[idx] > max:
										max = heatmap[idx]
									if heatmap[idx] < min:
										min = heatmap[idx]
					noOfMove += window.no_change


			# 4. update array
			#print max
			div_gap = 10
			if max != 0:
				div_gap = int(200.0 /max)
			buffer = array.array('B')
			for j in range(graph_height) : 
				for i in range(graph_width) : 
					idx = (j * graph_width) + i 
					ptn = heatmap[idx]
					if ptn > 0 : 
						# for local
						temp = 255 - int(255.0 * ptn / max)
						buffer.extend((temp, temp, temp))
					else:
						buffer.extend((255, 255, 255))

			# 5. create & draw bitmap
			rgbbmp = wx.BitmapFromBuffer(graph_width, graph_height, buffer)
			del buffer 
			dc.DrawBitmap(rgbbmp, x, y, False)

			i = min	
			range_loop = max - min + 1 
			gap = range_loop / 10.0
			if range_loop > 10 :
				range_loop = 10 
			if gap <= 0 : 
				gap = 1
			for j in range(range_loop) :
				temp = 255 - int(i * div_gap)
				dc.SetBrush(wx.Brush(wx.Colour(0, 0, temp)))
				dc.SetPen(wx.Pen(wx.Colour(0, 0, temp), 1))
				dc.DrawRectangle(x + graph_width + 10, y + (j*15), 15, 15)
				i += gap
			prev_height = y + graph_height + row_height +  10

			if self.space[0] == 1 :
				font = wx.Font(12, wx.TELETYPE, wx.NORMAL, wx.BOLD)
				dc.SetFont(font)
				x_label = x -110
				#y_label = (self.dim[1] * window_height) + 10 + (log_idx * label_height)
				y_label =  y

				dc.DrawText(self.log_date, x_label, y_label)

				dc.DrawText(desc1 + " " + desc2, x_label, y_label+20)
				windows_no = len(current_log.window_list)
				dc.DrawText("windows#: " + str(windows_no), x_label, y_label + 35)
				dc.DrawText("move#: " + str(noOfMove), x_label, y_label + 50)
				ave_move = 1.0 * noOfMove / windows_no
				dc.DrawText("(ave: " + str(ave_move) + ")", x_label, y_label + 65)

				dc.DrawText("start: " + str(self.start), x_label, y_label + 90)
				dc.DrawText("end: " + str(self.end), x_label, y_label+ 105)
				dc.DrawText("     /" + str(self.panel_end), x_label, y_label + 120)
			log_idx += 1 
			del heatmap

			# --- Section Log --- added by sanghwa #
                        self.Hflag = True
                        self.Vflag = True
			for section in current_log.section_list:
                            for sec in section.log_list:
                                if sec.mode == LOG_SECTION_NEW and not sec.direction == 3 :
                                    
				    print "section direction... ",  sec.direction
				    if sec.direction == HORIZONTAL :
                                        if self.Hflag :
                                            dc.DrawLines(((sec.left*secW_ratio+x, (graph_height-sec.top*secH_ratio)+y), (sec.right*secW_ratio+x, (graph_height-sec.top*secH_ratio)+y)))
                                            print "l,t,r,t", sec.left*secW_ratio+x, (graph_height-sec.top*secH_ratio)+y, sec.right*secW_ratio+x, (graph_height-sec.top*secH_ratio)+y
                                            self.Hflag = False
                                        else :
                                            self.Hflag = True
                                    else :
                                        if self.Vflag :
                                            dc.DrawLines(((sec.right*secW_ratio+x, (graph_height-sec.top*secH_ratio)+y), (sec.right*secW_ratio+x, (graph_height-sec.bottom*secH_ratio)+y)))
                                            self.Vflag = False
                                        else :
                                            self.Vflag = True

			


	def draw_merged_pointer(self, dc, window_width, window_height):
		no_user = len(self.parent.selected_user_list)
		no_log =  len(self.parent.log_list)
		if no_user == 1 and no_log > 1 :
			no_user = no_log

		foreground = self.parent.getForegroundColor()
		row_height = self.window.Height
		#if no_user > 0 : 
		#	row_height = int((self.window.Height - 61) / no_user)  

		filling_range = self.parent.effectRange
		half_range = int(filling_range /2)
		log_idx = 0 
		prev_height = 0
		for current_log in self.parent.log_list :

			ratio =  (current_log.display_width * 1.0) / current_log.display_height
			spare = int(window_width * 0.15)
			portion = 0.7
			if self.dim[0] == 1 or self.space[0] >= 2: 
				spare = 10 
				portion = 0.5

			graph_width = (window_width -spare) -50
			graph_height = int(graph_width / ratio)
			secW_ratio = graph_width*1.0/current_log.display_width
			secH_ratio = graph_height*1.0/current_log.display_height
			temp_height = window_height - 20
			label_height = window_height
			#if no_user > 0 :
			#	temp_height = int((window_height - (no_user +1) *10) / no_user)
			#	label_height = window_height / no_user
			if graph_height > temp_height: 
				graph_height = temp_height 
				graph_width =  int(graph_height * ratio)
			row_height =  row_height - graph_height  
			if self.space[0] == 1 :
				row_height = 0

			# 1. create buffer, 'B' means unsigned char, and create heatmap
			heatmap = []
			for j in range(graph_height) : 
				for i in range(graph_width) : 
					heatmap.append(0)


			log_end = current_log.duration
			self.log_date = current_log.date
			if self.panel_end == - 1 :
				self.end = -1
			if self.end == -1 or self.end > current_log.duration:
				self.end = current_log.duration

			x = (self.dim[0] * window_width) + int((window_width -graph_width) * portion)
			y = prev_height +  (self.dim[1] * window_height) + 10  + int(row_height / 2.0)
			max = 0
			min = 999

			# 2. clean heatmap data structure
			'''
			idx = 0
			for j in range(graph_height) : 
				for i in range(graph_width) : 
					heatmap[idx] = 0
					idx += 1
			'''
			dc.SetBrush(wx.TRANSPARENT_BRUSH)
			dc.SetPen(wx.Pen(foreground, 1))
			dc.DrawRectangle(x, y, graph_width, graph_height)

			for pointer in current_log.pointer_list: 
				# 3. update heatmap
				for log in pointer.log_list:
                                        if log.mode == LOG_POINTER_MOVE:
						log_min = log.time / 60
						if log_min < self.start or log_min > self.end :
							continue

						a = int(log.x * graph_width / current_log.display_width) - half_range 
						b = int(log.y * graph_height / current_log.display_height) - half_range
						for j in range(filling_range) : 
							b1 = b + j 
							if b1 < 0 or b1 >= graph_height: 
								continue
							for i in range(filling_range) : 
								a1 = a + i
								if a1 < 0 or a1 >= graph_width: 
									continue
								idx = (b1 * graph_width) + a1
								heatmap[idx] += 1
								if heatmap[idx] > max:
									max = heatmap[idx]
								if heatmap[idx] < min:
									min = heatmap[idx]

			# 4. update array
			#print max, min
			div_gap = 10
			if max != 0:
				div_gap = int(200.0 /max)
			buffer = array.array('B')
			for j in range(graph_height) : 
				for i in range(graph_width) : 
					idx = (j * graph_width) + i 
					ptn = heatmap[idx]
					if ptn > 0 : 
						# for local
						temp = 255 - int(255.0 * ptn / max)
						buffer.extend((temp, temp, temp))
					else:
						buffer.extend((255, 255, 255))

			# 5. create & draw bitmap
			rgbbmp = wx.BitmapFromBuffer(graph_width, graph_height, buffer)
			del buffer 
			dc.DrawBitmap(rgbbmp, x, y, False)

			i = min	
			range_loop = max - min + 1 
			gap = range_loop / 10.0
			if range_loop > 10 :
				range_loop = 10 
			if gap <= 0 : 
				gap = 1
			for j in range(range_loop) :
				temp = 255 - int(i * div_gap)
				dc.SetBrush(wx.Brush(wx.Colour(temp, 0, 0)))
				dc.SetPen(wx.Pen(wx.Colour(temp, 0, 0), 1))
				dc.DrawRectangle(x + graph_width + 10, y + (j*15), 15, 15)
				i += gap

			prev_height = y + graph_height + row_height +  10
			
			if self.space[0] == 1 :
				font = wx.Font(12, wx.TELETYPE, wx.NORMAL, wx.BOLD)
				dc.SetFont(font)
				#x = (self.window.Width -graph_width)/2  + graph_width + 70 
				x_label = x - 110
				y_label = (self.dim[1] * window_height) + 10 + (log_idx * label_height)
				dc.DrawText(self.log_date, x_label, y_label)

				dc.DrawText("mouse", x_label, y_label+20)
				dc.DrawText("movement", x_label, y_label + 35)

				dc.DrawText("start: " + str(self.start), x_label, y_label + 60)
				dc.DrawText("end: " + str(self.end), x_label, y_label + 75)
				dc.DrawText("     /" + str(self.panel_end), x_label, y_label + 90)
			log_idx += 1

			del heatmap
			for log in pointer.log_list:
                                if log.mode == LOG_POINTER_PCLICK:
                                        dc.SetBrush(wx.Brush(wx.Colour(128, 0, 0)))
                                        dc.SetPen(wx.Pen(wx.Colour(255,0,0), 1))
                                        dc.DrawCircle(log.x*secW_ratio+x, (graph_height-log.y*secH_ratio)+y,10)
                                        print "popup...",log.x*secW_ratio+x,log.y,(graph_height-log.y*secH_ratio)+y


        # point - color
	def draw_diff_pointer(self, dc, window_width, window_height):
		no_user = len(self.parent.selected_user_list)
		no_log =  len(self.parent.log_list)
		if no_user == 1 and no_log > 1 :
			no_user = no_log

		foreground = self.parent.getForegroundColor()
		row_height = self.window.Height
		#if no_user > 0 : 
		#	row_height = int((self.window.Height - 61) / no_user)  

		filling_range = self.parent.effectRange
		half_range = int(filling_range /2)
		log_idx = 0 
		prev_height = 0
		for current_log in self.parent.log_list :

			ratio =  (current_log.display_width * 1.0) / current_log.display_height
			spare = int(window_width * 0.15)
			portion = 0.7
			if self.dim[0] == 1 or self.space[0] >= 2: 
				spare = 10 
				portion = 0.5

			graph_width = (window_width -spare) -50
			graph_height = int(graph_width / ratio)
			temp_height = window_height - 20
			label_height = window_height
			#if no_user > 0 :
			#	temp_height = int((window_height - (no_user +1) *10) / no_user)
			#	label_height = window_height / no_user
			if graph_height > temp_height: 
				graph_height = temp_height 
				graph_width =  int(graph_height * ratio)
			row_height =  row_height - graph_height  
			if self.space[0] == 1 :
				row_height = 0

			# 1. create buffer, 'B' means unsigned char, and create heatmap
			heatmap_list = []
			for pointer in current_log.pointer_list: 
				heatmap = []
				for j in range(graph_height) : 
					for i in range(graph_width) : 
						heatmap.append(0)
				heatmap_list.append(heatmap)


			log_end = current_log.duration
			self.log_date = current_log.date
			if self.panel_end == - 1 :
				self.end = -1
			if self.end == -1 or self.end > current_log.duration:
				self.end = current_log.duration

			x = (self.dim[0] * window_width) + int((window_width -graph_width) * portion)
			y = prev_height +  (self.dim[1] * window_height) + 10  + int(row_height / 2.0)
			max = 0
			min = 999

			# 2. clean heatmap data structure
			'''
			idx = 0
			for j in range(graph_height) : 
				for i in range(graph_width) : 
					heatmap[idx] = 0
					idx += 1
			'''
			dc.SetBrush(wx.TRANSPARENT_BRUSH)
			dc.SetPen(wx.Pen(foreground, 1))
			dc.DrawRectangle(x, y, graph_width, graph_height)

			jdx = 0
			for pointer in current_log.pointer_list: 
				heatmap = heatmap_list[jdx]

				# 3. update heatmap
				for log in pointer.log_list: 
 					if log.mode == LOG_POINTER_MOVE:
						log_min = log.time / 60
						if log_min < self.start or log_min > self.end :
							continue

						a = int(log.x * graph_width / current_log.display_width) - half_range 
						b = int(log.y * graph_height / current_log.display_height) - half_range
						for j in range(filling_range) : 
							b1 = b + j 
							if b1 < 0 or b1 >= graph_height: 
								continue
							for i in range(filling_range) : 
								a1 = a + i
								if a1 < 0 or a1 >= graph_width: 
									continue
								idx = (b1 * graph_width) + a1
								heatmap[idx] = 255 
								if heatmap[idx] > max:
									max = heatmap[idx]
								if heatmap[idx] < min:
									min = heatmap[idx]
				jdx += 1

			# 4. update array
			#print max, min
			div_gap = 10
			buffer = array.array('B')
			for j in range(graph_height) : 
				for i in range(graph_width) : 
					idx = (j * graph_width) + i 
					heatmap = heatmap_list[0]
					r = heatmap[idx]
					heatmap = heatmap_list[1]
					g = heatmap[idx]
					heatmap = heatmap_list[2]
					b = heatmap[idx]
					'''
					#heatmap = heatmap_list[0]
					#r = heatmap[idx]
					r = 0
					heatmap = heatmap_list[0]
					g = heatmap[idx]
					heatmap = heatmap_list[1]
					b = heatmap[idx]
					'''
					if r == 0 and g == 0 and b == 0 : 
						buffer.extend((255, 255, 255))
					elif r == 255 and g == 255 and b == 255 : 
						buffer.extend((0, 0, 0))
					else:
						buffer.extend((r, g, b))

			# 5. create & draw bitmap
			rgbbmp = wx.BitmapFromBuffer(graph_width, graph_height, buffer)
			del buffer 
			dc.DrawBitmap(rgbbmp, x, y, False)

			i = min	
			range_loop = max - min + 1 
			gap = range_loop / 10.0
			if range_loop > 10 :
				range_loop = 10 
			if gap <= 0 : 
				gap = 1

			prev_height = y + graph_height + row_height +  10
			
			if self.space[0] == 1 :
				font = wx.Font(12, wx.TELETYPE, wx.NORMAL, wx.BOLD)
				dc.SetFont(font)
				#x = (self.window.Width -graph_width)/2  + graph_width + 70 
				x_label = x - 110
				y_label = (self.dim[1] * window_height) + 10 + (log_idx * label_height)
				dc.DrawText(self.log_date, x_label, y_label)

				dc.DrawText("mouse", x_label, y_label+20)
				dc.DrawText("movement", x_label, y_label + 35)

				dc.DrawText("start: " + str(self.start), x_label, y_label + 60)
				dc.DrawText("end: " + str(self.end), x_label, y_label + 75)
				dc.DrawText("     /" + str(self.panel_end), x_label, y_label + 90)
			log_idx += 1

			del heatmap
			del heatmap_list


